﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.IO;
using System.Web.Routing;
using System.Diagnostics;

using DataAccess;

namespace SQT.tracnghiem
{
    public class Global : System.Web.HttpApplication
    {
        void RegisterRoutes(RouteCollection routes)
        {
            routes.MapPageRoute("Login", "login", "~/Login.aspx");
            routes.MapPageRoute("DefaultAdmin", "quan-ly/tong-quan", "~/Admin/DefaultAdmin.aspx");
            routes.MapPageRoute("Gallery", "quan-ly/thu-vien", "~/Admin/Gallery.aspx");
            var get_submenu = new TblAdminMenuController().FetchAll().Where(x => x.AmIsDelete == false && x.AmParentID != 0);
            foreach(TblAdminMenu submenu in get_submenu)
            {
                routes.MapPageRoute(submenu.AmName, submenu.AmRouting, submenu.AmUrl);
            }
            routes.MapPageRoute("Staff_Personal", "quan-ly/trang-ca-nhan-{id}", "~/Admin/Staff_Personal.aspx");
            routes.MapPageRoute("Home", "", "~/Default.aspx");
            routes.MapPageRoute("Alphabets", "bang-chu-cai", "~/Alphabets.aspx");
            routes.MapPageRoute("About", "gioi-thieu", "~/About.aspx");
            routes.MapPageRoute("Study_Weekly", "lich-hoc", "~/Study_Weekly.aspx");
            routes.MapPageRoute("News", "tin-tuc", "~/BaiViet/News.aspx");
            routes.MapPageRoute("Authentication", "dang-nhap", "~/Authentication.aspx");
            routes.MapPageRoute("Personal", "trang-ca-nhan-{id}", "~/Personal.aspx");
            routes.MapPageRoute("Multiple-choice", "luyen-thi", "~/TracNghiem/Multiple-choice.aspx");
            routes.MapPageRoute("Result", "luyen-thi/ket-qua-{id}", "~/TracNghiem/Result.aspx");
            routes.MapPageRoute("Task", "luyen-thi/{nameEn}-{id}", "~/TracNghiem/Task.aspx");
            routes.MapPageRoute("404", "not-found", "~/404error.html");
            routes.MapPageRoute("Lesson", "bai-hoc", "~/GiaoAn/Lesson.aspx");
            routes.MapPageRoute("PartOfLesson", "bai-hoc/{nameEn}-{id}", "~/GiaoAn/PartOfLesson.aspx");
            routes.MapPageRoute("NewsCategories", "danh-muc/{nameEn}-{id}", "~/BaiViet/NewsCategories.aspx");
            routes.MapPageRoute("NewsReading", "bai-viet/{nameEn}-{id}", "~/BaiViet/NewsReading.aspx");
            routes.MapPageRoute("NewsTags", "tags/{nameEn}-{id}", "~/BaiViet/NewsTags.aspx");
            routes.MapPageRoute("NewsSearch", "tim-kiem/tu-khoa", "~/BaiViet/NewsSearch.aspx");
            routes.MapPageRoute("Contact", "lien-he", "~/Contact.aspx");
            routes.MapPageRoute("Event", "giai-tri/su-kien", "~/GiaiTri/SuKien/Event.aspx");
            routes.MapPageRoute("Player", "giai-tri/su-kien/nguoi-choi", "~/GiaiTri/SuKien/Event_Player.aspx");
            routes.MapPageRoute("Classify", "chao-mung-ban", "~/Classify.aspx");
            routes.MapPageRoute("LessonTest", "kiem-tra", "~/Lesson_Test.aspx");
        }

        protected void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes(RouteTable.Routes);
            // Code that runs on application startup
            string path = Server.MapPath("~") + "\\count.txt";
            if (!File.Exists(path))
                File.WriteAllText(path, "0");

            Application["Access"] = int.Parse(File.ReadAllText(path));
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            string path = Server.MapPath("~") + "\\count.txt";
            // Code that runs when a new session is started
            if (Application["Online"] == null)
                Application["Online"] = 1;
            else
                Application["Online"] = (int)Application["Online"] + 1;

            Application["Access"] = (int)Application["Access"] + 1;
            File.WriteAllText(path, Application["Access"].ToString());
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Exception objErr = Server.GetLastError().GetBaseException();
            string err = "Error Caught in Application_Error event\n" +
                    "Error in: " + Request.Url.ToString() +
                    "\nError Message:" + objErr.Message.ToString() +
                    "\nStack Trace:" + objErr.StackTrace.ToString();
            EventLog.WriteEntry("Sample_WebApp", err, EventLogEntryType.Error);
        }

        protected void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.
            int i = (int)Application["Online"];
            if (i > 1)
                Application["Online"] = i - 1;

            string path = Server.MapPath("~") + "\\count.txt";
            File.WriteAllText(path, Application["Access"].ToString());
        }

        protected void Application_End(object sender, EventArgs e)
        {
            // Code that runs on application shutdown
            string path = Server.MapPath("~") + "\\count.txt";

            File.WriteAllText(path, Application["Access"].ToString());
        }

    }
}